package com.cjie.ecommerce.controller;

import com.cjie.ecommerce.entity.Product;
import com.cjie.ecommerce.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 获取所有有效产品
     * 示例URL：http://localhost:8080/api/products/get-all-products
     *
     * @return
     */
    @GetMapping("/get-all-products")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

    /**
     * 根据ID获取商品详情
     * 示例URL：http://localhost:8080/api/products/get-product-by-id/1
     *
     * @param id
     * @return
     */
    @GetMapping("/get-product-by-id/{id}")
    public Product getProductById(@PathVariable Long id) {
        return productService.getProductById(id);
    }

    /**
     * 更新商品信息
     *
     * @param product 商品信息
     */
    @PutMapping("/update-product")
    public void updateProduct(@RequestBody Product product) {
        productService.updateProduct(product);
    }

    /**
     * 根据id删除某个商品信息
     *
     * @param id 商品id
     */
    @DeleteMapping("/delete-product/{id}")
    public void deleteProduct(@PathVariable Long id) {
        productService.deleteProduct(id);
    }
}